﻿namespace Shared.Crypto
{
    public class Password : IPasswordHash
    {
        private const int WorkFactor = 12;

        public string Hash(string plainPassword)
        {
            if (string.IsNullOrWhiteSpace(plainPassword))
                throw new ArgumentException("密码不能为空", nameof(plainPassword));

            return BCrypt.Net.BCrypt.HashPassword(plainPassword, WorkFactor);
        }

        public bool Verify(string plainPassword, string hashedPassword)
        {
            if (string.IsNullOrEmpty(hashedPassword))
                return false;

            //防时序攻击的，还不能用.Equals
            return BCrypt.Net.BCrypt.Verify(plainPassword, hashedPassword);
        }
    }
}
